package com.ydgk.servlet;

import com.alibaba.fastjson.JSON;
import com.ydgk.bean.Bill;
import com.ydgk.bean.Commodity;
import com.ydgk.dao.BillDao;
import com.ydgk.dao.UserDao;
import com.ydgk.service.BillService;
import com.ydgk.service.CommService;
import com.ydgk.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "BIllServlet", urlPatterns = "/BIllServlet.action")
public class BIllServlet extends BaseServlet {
    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BillService billService = new BillService();
        List<Bill> billList= billService.findAll();
        request.setAttribute("billList",billList);
        request.setAttribute("mainPage","billFindAll.jsp");
        request.getRequestDispatcher("index.jsp").include(request,response);
    }

    protected void findBillAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        BillService billService = new BillService();
        int maxPage = billService.maxPage("bill");
        String pn = request.getParameter("pageNumber");
        String pc = request.getParameter("pageCount");
        int pageNumber = 1;
        int pageCount = 8 ;
        try {
            pageNumber = Integer.parseInt(pn);
            pageCount = Integer.parseInt(pc);
        }catch (NumberFormatException e){
            pageNumber = 1;
            pageCount = 8 ;
        }
        BillDao billDao = new BillDao();
        billDao.pageCount = pageCount;

        if(pageNumber <1){
            pageNumber=1;
        }

        if(pageNumber > maxPage){
            pageNumber = maxPage ;
        }

        List<Bill> billList = billService.paging(pageNumber);
        int total = billService.total();
        HashMap map = new HashMap() ;

        map.put("code",0);
        map.put("msg","");
        map.put("data",billList);
        map.put("pageCount",pageCount);
        map.put("pageNumber",pageNumber);
        map.put("total",total);

        String str = JSON.toJSONString(map);

        response.getWriter().print(str);

    }

}
